<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" :width="600"  destroy-on-close @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
      <el-form-item label="岗位名称"  class="inline" prop="postName">
        <el-input v-model="form.postName" placeholder="请输入岗位名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="岗位编码" class="inline" prop="postCode">
        <el-input v-model="form.postCode" placeholder="请输入岗位编码" clearable></el-input>
      </el-form-item>
      <el-form-item label="排序" class="inline" prop="sort">
        <el-input v-model.number="form.sort" placeholder="请输入岗位排序" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否启用"  prop="enabled">
        <el-radio-group v-model="form.enabled">
          <el-radio
            v-for="item in dicts.system_global_status"
            :key="item.v"
            :label="form.enabled == item.v ? form.enabled : item.v "
            :value="item.v"
          >
            {{ item.k }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.address" placeholder="请输入岗位备注" clearable type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible=false" >取 消</el-button>
      <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
export default {
  computed: {
    id() {
      return id
    }
  },
  dicts: ['system_global_status'],
  emits: ['success', 'closed'],
  data() {
    return {
      mode:'add',
      titleMap: {
        add: '新增岗位',
        edit: '编辑岗位',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      //表单数据
      form: {
        id:"",
        postName: "",
        postCode: "",
        sort: null,
        enabled: "1",
        remark: ""
      },
      //验证规则
      rules: {
        postName: [
          {required: true, message: '岗位名称不能为空！',trigger:'blur'},
        ],
        postCode: [
          {required: true, message: '岗位编码不能为空',change:'blur'},
        ],
        sort: [
          {required: true, message: '排序不能为空',change:'blur',type:'number'},
        ],
      }
    }
  },
  methods: {
    //点击节点
    handleNodeClick(data,checked){
      if(checked){
        this.$refs.tree.setCheckedNodes([data])
      }
    },
    //显示
    open(mode='add'){
      this.mode = mode;
      this.visible = true;
      return this
    },
    //表单提交方法
    submit(){
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          let res = {};
          if(this.mode == 'add'){
            res = await this.$API.system.post.addPost.post(this.form)
          } else {
            res = await this.$API.system.post.updatePost.post(this.form);
          }
          this.isSaveing = false;
          if(res.code == 200){
            this.$emit('success', this.form, this.mode)
            this.visible = false;
            this.$message.success()
          }else{
            this.$alert(res.message, "提示", {type: 'error'})
          }
        }
      })
    },
    //表单注入数据
    async setData(id){
      if(id){
        let res = await this.$API.system.post.detail.get(id)
        this.form = res.data
      } else{
        this.form.enabled = 1;
      }

    }
  }
}
</script>

<style scoped>
.inline{
  display: inline-flex;
  vertical-align: middle;
  width: 50%;
}
.-input__inner{
  width: 100%;
}
</style>
